package yxy.game.pm2.active.command.sdk.weixin.invitefriends;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actyaoqingjiangli;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.sdk.weixin.ActiveModule_邀请好友;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.SdkActiveMessage;
import yxy.game.pm2.message.SdkActiveMessage.WeiXinInviteFriendsDrawRewardRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

/**
 * 邀请好友
 * @author Dell
 *
 */
@MessageType(proto = Command.WeiXinInviteFriendsDrawReward, classtype = SdkActiveMessage.WeiXinInviteFriendsDrawRewardRequest.class)
public class CMD_Active_邀请好友_DrawReward extends Command<WeiXinInviteFriendsDrawRewardRequest> {
	
	private static final Logger logger = Logger.create(CMD_Active_邀请好友_DrawReward.class);
	
	@Override
	public void execute(MessageSource messageSource, WeiXinInviteFriendsDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		int id = request.getId();
		SdkActiveMessage.WeiXinInviteFriendsDrawRewardResponse.Builder response = SdkActiveMessage.WeiXinInviteFriendsDrawRewardResponse.newBuilder();
		
		ActiveModule_邀请好友 am = ActiveModule_邀请好友.get(pcode);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
				
		actyaoqingjiangli data = ActDataCenter.getData(id, actyaoqingjiangli.class, pcode);
		if(data == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(am.getDrawids().contains(id)){
			send(messageSource, response, Result.ActHadGet);
			return;
		}

		am.getDrawids().add(id);
		am.set();
		
		Collection<Currency> curs = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
		response.addAllDisplay(items);
		
		new CurrencyHandle().addCurrency(pcode, CCReason.微信_活动_邀请好友, curs);
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, SdkActiveMessage.WeiXinInviteFriendsDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
}
